import importlib

from scipy.ndimage import gaussian_filter
import scipy.ndimage as ndimg

('scipy.ndimage', '', 'ndimg')

def parse_import(line):
    if 'from' in line:
        pk, f = line.split('from')[1].split('import')
        ims = [i.split(' as ')*2 for i in f.split(',')]
        ims = [(pk.strip(), i[0].strip(), i[1].strip()) for i in ims]
        return ims
    else:
        ims = line.split('import')[1].split(',')
        ims = [i.split(' as ')*2 for i in ims]
        ims = [(i[0].strip(), '', i[1].strip()) for i in ims]
        return ims


def import_model(pg, func, name):
    try:
        rst = importlib.__import__(pg, fromlist=[func])
        if func != '': rst = getattr(rst, func)
        name = pg if func is '' else func
        fields = dir(rst)
        isclass = lambda x: 'class' in str(getattr(rst, x))[:8]
        classes = {}
        for i in fields:
            obj = getattr(rst, i)
            if 'class' in str(obj)[:8]:
                classes[name+'.'+i] = dir(obj)
        return name, fields, classes
    except: return None, [], []
    
if __name__ == '__main__':
    a = parse_import('import numpy as np, scipy as sp')
    b = parse_import('from scipy.ndimage import gaussian_filter as gf, binary')
    c = parse_import('import scipy.ndimage')

    a = import_model('scipy.ndimage', '', 'ndimg')
    
